#include <gtk/gtk.h>
#include <math.h>
#include "graph.h"

#define DISPLAY_CONTROL_GUI

static GtkWidget *speed, *min, *max;
static GtkWidget *channel, *channel_name, *color, *set, *start, *stop, *update, *clear;

static void set_button_clicked( GtkWidget *widget,
                   gpointer   data )
{
	MonitorGraph *g = (MonitorGraph*)data;
	gint vspeed, vmin, vmax, vchannel;
	gchar *vchannel_name, *vcolor;

	vspeed = atoi(gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (speed)->entry)));
	vmin = atoi(gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (min)->entry)));
	vmax = atoi(gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (max)->entry)));
	vchannel = atoi(gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (channel)->entry)));
	vchannel_name = (gchar*)gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (channel_name)->entry));
	vcolor = (gchar*)gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (color)->entry));

	g_print("speed=%d, min=%d, max=%d\n", vspeed, vmin, vmax);
	g_print("channel=%d, name=%s, color=%s\n", vchannel, vchannel_name, vcolor);

	monitor_graph_set_polling_speed(g, (guint)vspeed);
	monitor_graph_set_data(g, (gfloat)vmin, (gfloat)vmax);
	monitor_graph_set_channel(g, vchannel, vchannel_name, vcolor);

}

static void start_button_clicked( GtkWidget *widget,
                   gpointer   data )
{
	MonitorGraph *g = (MonitorGraph*)data;

	monitor_graph_polling_start(g);
}

static void stop_button_clicked( GtkWidget *widget,
                   gpointer   data )
{
	MonitorGraph *g = (MonitorGraph*)data;

	monitor_graph_polling_stop(g);
}

static void update_button_clicked( GtkWidget *widget,
                   gpointer   data )
{
	MonitorGraph *g = (MonitorGraph*)data;

	monitor_graph_force_update(g);
}

static void clear_button_clicked( GtkWidget *widget,
                   gpointer   data )
{
	MonitorGraph *g = (MonitorGraph*)data;

	monitor_graph_data_clear(g);
}

gint graph1_callback(MonitorGraph *g, guint channel, gfloat *data)
{
	GRand *rand;
	gint r;

	rand = g_rand_new ();
	switch (channel) {
	case 1:
		r = g_rand_int_range(rand, -3000, 5000);
		break;
	case 2:
		r = g_rand_int_range(rand, -7000, 2000);
		break;
	default:
		r = g_rand_int_range(rand, 5000, 7000);
		break;
	}
	g_rand_free(rand);
	*data = (gfloat)r;

	return MONITOR_GRAPH_SUCCESS;
}


int main(int argc, char **argv)
{
	GtkWidget *window;
	GtkWidget *label, *draw;
	GtkWidget *vBox, *vBox1, *vBox2, *hBox1, *hBox2, *hBox3;
	GtkWidget *hhBox1, *hhBox2, *hhBox3, *hhBox4, *hhBox5, *hhBox6, *hhBox7, *hhBox8;
	GtkWidget *vvBox1, *vvBox2, *vvBox3;
	MonitorGraph graph1;

	gtk_init(&argc, &argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW(window),"vBox partition and draw widget demo 2010_0510");
	gtk_window_set_default_size (GTK_WINDOW(window), 400, 360);

	vBox = gtk_vbox_new(FALSE, 10);

	gtk_container_add (GTK_CONTAINER(window), vBox);

	vBox1 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vBox), vBox1, TRUE, TRUE, 0);

	monitor_graph_init(&graph1, window, 3, 0, graph1_callback);
	monitor_graph_set_polling_speed(&graph1, 500);
	monitor_graph_set_data(&graph1, -7000, 7000);
	monitor_graph_polling_start(&graph1);
#ifndef DISPLAY_CONTROL_GUI
	gtk_box_pack_start (GTK_BOX(vBox1), monitor_graph_get_widget(&graph1), 
			    TRUE, TRUE, 0);
#else
	hBox1 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vBox1), hBox1, FALSE, FALSE, 0);
	hBox2 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vBox1), hBox2, TRUE, TRUE, 0);


	vvBox1 = gtk_vbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hBox1), vvBox1, TRUE, TRUE, 0);
	vvBox2 = gtk_vbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hBox1), vvBox2, TRUE, TRUE, 0);
	vvBox3 = gtk_vbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(hBox1), vvBox3, TRUE, TRUE, 0);

	hhBox1 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox1, FALSE, FALSE, 0);
	hhBox2 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox2, FALSE, FALSE, 0);
	hhBox3 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox3, FALSE, FALSE, 0);
	hhBox4 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox4, FALSE, FALSE, 0);
	hhBox5 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox5, FALSE, FALSE, 0);
	hhBox6 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox6, FALSE, FALSE, 0);
	hhBox7 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox7, FALSE, FALSE, 0);
	hhBox8 = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX(vvBox1), hhBox8, FALSE, FALSE, 0);

	label = gtk_label_new("Speed(ms)");
	gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	speed = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(speed)->entry), "500");
	gtk_widget_set_size_request(GTK_WIDGET (speed), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox1), label, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX(hhBox1), speed, FALSE, FALSE, 0);

	label = gtk_label_new ("Scale(Min)");
	gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox2), label, FALSE, FALSE, 0);
	min = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(min)->entry), "-10000");
	gtk_widget_set_size_request (GTK_WIDGET (min), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox2), min, FALSE, FALSE, 0);
	label = gtk_label_new ("Scale(Max)");
		gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox3), label, FALSE, FALSE, 0);
	max = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(max)->entry), "10000");
	gtk_widget_set_size_request (GTK_WIDGET (max), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox3), max, FALSE, FALSE, 0);

	label = gtk_label_new ("Channel");
	gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox4), label, FALSE, FALSE, 0);
	channel = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(channel)->entry), "1");
	gtk_widget_set_size_request (GTK_WIDGET (channel), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox4), channel, FALSE, FALSE, 0);

	label = gtk_label_new ("Name");
	gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox5), label, FALSE, FALSE, 0);
	channel_name = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(channel_name)->entry), "Victor");
	gtk_widget_set_size_request (GTK_WIDGET (channel_name), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox5), channel_name, FALSE, FALSE, 0);

	label = gtk_label_new ("Color");
	gtk_widget_set_size_request (GTK_WIDGET (label), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox6), label, FALSE, FALSE, 0);
	color = gtk_combo_new ();
	gtk_entry_set_text (GTK_ENTRY(GTK_COMBO(color)->entry), "#FF0000");
	gtk_widget_set_size_request (GTK_WIDGET (color), 80, -1);
	gtk_box_pack_start (GTK_BOX(hhBox6), color, FALSE, FALSE, 0);

	set = gtk_button_new_with_label ("Set");
	gtk_widget_set_size_request (GTK_WIDGET (set), 52, -1);
	gtk_box_pack_start (GTK_BOX(hhBox7), set, FALSE, FALSE, 0);

	start = gtk_button_new_with_label ("Start");
	gtk_widget_set_size_request (GTK_WIDGET (start), 52, -1);
	gtk_box_pack_start (GTK_BOX(hhBox7), start, FALSE, FALSE, 0);

	stop = gtk_button_new_with_label ("Stop");
	gtk_widget_set_size_request (GTK_WIDGET (stop), 52, -1);
	gtk_box_pack_start (GTK_BOX(hhBox7), stop, FALSE, FALSE, 0);

	update = gtk_button_new_with_label ("Force Update");
	gtk_widget_set_size_request (GTK_WIDGET (update), 110, -1);
	gtk_box_pack_start (GTK_BOX(hhBox8), update, FALSE, FALSE, 0);

	clear = gtk_button_new_with_label ("Clear");
	gtk_widget_set_size_request (GTK_WIDGET (clear), 52, -1);
	gtk_box_pack_start (GTK_BOX(hhBox8), clear, FALSE, FALSE, 0);

	gtk_box_pack_start (GTK_BOX(hBox2), monitor_graph_get_widget(&graph1), 
			    TRUE, TRUE, 0);
	g_signal_connect (set, "clicked",
		      G_CALLBACK (set_button_clicked), &graph1);
	g_signal_connect (start, "clicked",
		      G_CALLBACK (start_button_clicked), &graph1);
	g_signal_connect (stop, "clicked",
		      G_CALLBACK (stop_button_clicked), &graph1);
	g_signal_connect (update, "clicked",
		      G_CALLBACK (update_button_clicked), &graph1);
	g_signal_connect (clear, "clicked",
		      G_CALLBACK (clear_button_clicked), &graph1);
#endif

    g_signal_connect (window, "destroy",
			  G_CALLBACK (gtk_main_quit), NULL);

	gtk_widget_show_all (window);
	gtk_main();
}
